"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin
Case Name   : C库、PG库下测试join多表查询，不带on，合理报错
Create At   : 2022.04.06
Owner       : z@wan005
Description :
    1、创建c库和PG库
    2、C库下建2张表
    3、插入数据
    4、使用join两个表查询，不加on
    5、PG库下建2张表
    6、插入数据
    7、切换到PG库下使用join查询，不加on条件
    8、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、报错
    5、成功
    6、成功
    7、报错
    8、清理环境完成
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class PluginTest08(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_plugin0008'
        self.db_name_01 = 'db_plugin0008_01'
        self.tb_name = 'tb_plugin0008'

    def test_dolphin(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1:创建c库和PG库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} DBCOMPATIBILITY ='C';" \
                  f"create database {self.db_name_01} DBCOMPATIBILITY ='PG';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEquals(2,
                          sql_res.count(self.constant.CREATE_DATABASE_SUCCESS),
                          '执行失败' + text)

        text = '--step2:C库下建2张表;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name},{self.tb_name}_01;
            create table {self.tb_name}(i integer,j integer,t text);\
            create table {self.tb_name}_01(i integer,k integer);'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEquals(2, sql_res.count(self.constant.TABLE_CREATE_SUCCESS),
                          '执行失败' + text)

        text = '-----step3:插入数据;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''insert into {self.tb_name} values (1, 4, 'one');
            insert into {self.tb_name} values (2, 3, 'two');
            insert into {self.tb_name} values (0, null, 'zero');
            insert into {self.tb_name}_01 values (1, -1);
            insert into {self.tb_name}_01 values (null, 0);'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEquals(5, sql_res.count(self.constant.INSERT_SUCCESS_MSG),
                          '执行失败:' + text)

        text = '--step4:使用join两个表查询，不加on;expect:报错---'
        self.log.info(text)
        sql_cmd = f'''select '' as "xxx", * from {self.tb_name} join \
            {self.tb_name}_01 order by {self.tb_name}.i;\
            select '' as "xxx", * from {self.tb_name} inner join \
            {self.tb_name}_01;'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('ERROR', sql_res, '执行失败' + text)

        text = '--step5:PG库下建2张表;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name},{self.tb_name}_01;
           create table {self.tb_name}(i integer,j integer,t text);\
           create table {self.tb_name}_01(i integer,k integer);'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name_01)
        self.log.info(sql_res)
        self.assertEquals(2, sql_res.count(self.constant.TABLE_CREATE_SUCCESS),
                          '执行失败' + text)

        text = '-----step6:插入数据;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''insert into {self.tb_name} values (1, 4, 'one');
                   insert into {self.tb_name} values (2, 3, 'two');
                   insert into {self.tb_name} values (0, null, 'zero');
                   insert into {self.tb_name}_01 values (1, -1);
                   insert into {self.tb_name}_01 values (null, 0);'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name_01)
        self.log.info(sql_res)
        self.assertEquals(5, sql_res.count(self.constant.INSERT_SUCCESS_MSG),
                          '执行失败:' + text)

        text = '--step7:切换到PG库下使用join查询，不加on条件;expect:报错---'
        self.log.info(text)
        sql_cmd = f'''select '' as "xxx", * from {self.tb_name}  \
               join {self.tb_name}_01 join {self.tb_name}_02;\
               select '' as "xxx", * from {self.tb_name}  inner join \
               {self.tb_name}_01 join {self.tb_name}_02;'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name_01)
        self.log.info(sql_res)
        self.assertIn('ERROR', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '--step8:清理环境;expect:清理环境完成--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"drop database if exists {self.db_name}_01;"
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEquals(2,
                          sql_res.count(self.constant.DROP_DATABASE_SUCCESS),
                          '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
